<?php namespace Admin\Controller;
use \Common\Model\Shop_attribute;
use \Common\Model\Shop;
class AttributeController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Attribute;
	}
	public function index(){

		$type_tid=Q('get.tid',0,'intval');
		$data=$this->model->where("type_tid={$type_tid}")->get();
		View::with('data',$data);
		View::make();
	}
	public function add(){
		if(IS_POST){
//			p($_POST);exit;
			
//			$data=$_POST['tname'];
			$data=Q('post.aname');
			$a=Q("post.");
//			p($data);exit;
			$oldData=$this->model->where("aname='{$data}'")->find();
			if($oldData){
				View::success("属性已存在");
			}
			if($this->model->store()){
				View::success('添加成功',U('Attribute/index')."&tid={$a['type_tid']}");
			}
			View::error($this->model->getError());
		}
		View::make();

	}
	//	编辑
	public function edit(){
		$aid=Q('get.aid',0,'intval');
//		p($aid);exit;
		if(IS_POST){
			$this->model->where("aid={$aid}")->update();
			View::success('修改成功',U('Type/index'));
		}
		$data=$this->model->where("aid={$aid}")->find();
//		p($data);
		View::with('data',$data);
		View::make();
	}
	//	删除
	public function del(){
		$aid=Q('get.aid',0,'intval');
		$this->model->where("aid={$aid}")->delete();	
		$Shop_attributeModel=new Shop_attribute;
		$Shop_attributeModel->where("attribute_aid={$aid}")->delete();
		$sid=$Shop_attributeModel->where("attribute_aid={$aid}")->pluck('shop_id');	
		$ShopModel=new Shop;
		if(!$sid){
			$sid=[];
		}
		foreach($sid as $v){
			$ShopModel->where("sid={$v}")->delete();
		}
		
		View::success('删除成功');
	}
}
